// ==++==
// 
//   
//    Copyright (c) 2006 Microsoft Corporation.  All rights reserved.
//   
//    The use and distribution terms for this software are contained in the file
//    named license.txt, which can be found in the root of this distribution.
//    By using this software in any fashion, you are agreeing to be bound by the
//    terms of this license.
//   
//    You must not remove this notice, or any other, from this software.
//   
// 
// ==--==

////////////////////////////////////////////////////////////////////////////////
// FILE_CAN.H

#ifndef __FILE_CAN_H__
#define __FILE_CAN_H__

#include "unicase.h"

enum FileType
{
    ftUnknown = 0,
    ftUnicode, 
    ftSwappedUnicode, 
    ftUTF8, 
    ftASCII,
    ftBinary
};

HANDLE OpenFileEx( LPCWSTR filename, DWORD *fileLen, LPCWSTR relPath = NULL, bool bWrite = false);
HRESULT ReadTextFile (PCWSTR pszFileName, UINT uiCodePage, WCAllocBuffer & textBuffer, FileType *fileType);

// Src and Dest may be the same buffer
// Returns 0 for error (check via GetLastError()) or count of characters
// (not including NULL) copied to Dest.
// if fPreserveSrcCasing is set, ignores on-disk casing of filename (but still gets on-disk casing of directories) 
// if fPreserveSrcCasing is set and and existing file matches with different short/longness it will fail
// and set the error code to ERROR_FILE_EXISTS
DWORD GetCanonFilePath(LPCWSTR wszSrcFileName, const WCBuffer outBuffer, bool fPreserveSrcCasing);

// Remove quote marks from a string.
// Translation is done in-place
LPWSTR RemoveQuotes(WCBuffer textBuffer);

// Remove quote marks from a string.
// Replace various characters with other illegal characters if unquoted.
// Translation is done in-place.
LPWSTR RemoveQuotesAndReplaceComma(WCBuffer textBuffer);        // ","  -> "|"
LPWSTR RemoveQuotesAndReplacePathDelim(WCBuffer textBuffer);    // ",;" -> "|"
LPWSTR RemoveQuotesAndReplaceAlias(WCBuffer textBuffer);        // ",;" -> "|" and "=" -> "\x1"

// Safe version of ToLowerCase
// Gaurantees null termination even if buffer size is too small
inline PWSTR   WINAPI  SafeToLowerCase    (PCWSTR pSrc, WCBuffer textBuffer)
{
    PWSTR returnValue = ToLowerCase(pSrc, textBuffer.GetData(), textBuffer.Count());
    if (textBuffer.Count() > 0) {
        textBuffer.SetAt(textBuffer.Count() - 1, 0);
    }
    return returnValue;
}

// Joins a relative or absolute filename to the given path and stores the new
// filename in lpBuffer
bool MakePath(/*[in]*/LPCWSTR lpPath, /*[in]*/LPCWSTR lpFileName, const WCBuffer pathBuffer);

#endif // __FILE_CAN_H__
